import {
  formatHour
} from '../../utils/util.js'
const app = getApp()
Page({
  data: {
    bannerList: [],
    categoryList: [],
    selectedTab: {},
    courseList: [],
    originalCourseList: [],
    height: 0,
    pageNum: 1,
    pageSize: 10,
    isRequest: true // 是否继续请求
  },
  onLoad() {
    const sys = wx.getSystemInfoSync();
    this.setData({
      height: sys.windowHeight - 308
    })
  },
  onShow() {
    if (app.checkUserLogin()) {
      this.getBanner();
      this.getCourseCategory();
    }
  },
  getBanner() {
    app.fetchRequest({
      url: '/course/banner/list',
      data: {},
      success: (res) => {
        this.setData({
          bannerList: res.list
        })
      }
    })
  },
  // banner点击
  tapBanner(e) {
    const item = e.currentTarget.dataset.item;
    if (item.courseSeq) {
      wx.navigateTo({
        url: `../course/course?courseId=${item.courseSeq}`,
      })
    } else {
      wx.navigateTo({
        url: `../webview/webview?url=${item.linkAddress}`,
      })
    }
  },
  // 获取课程类别
  getCourseCategory() {
    app.fetchRequest({
      url: '/course/category',
      data: {},
      method: 'get',
      success: (res) => {
        wx.stopPullDownRefresh()
        this.setData({
          categoryList: res,
          selectedTab: res[0]
        })
        this.getAllCourse(res[0].categoryId);
      }
    })
  },
  changeCategory(e) {
    const index = e.currentTarget.dataset.index;
    const categoryId = this.data.categoryList[index].categoryId
    this.setData({
      selectedTab: this.data.categoryList[index],
      pageNum: 1,
      courseList: [],
      originalCourseList: [],
      isRequest: true
    });
    this.getAllCourse(categoryId);
  },
  // 获取所有课程
  getAllCourse(categoryId) {
    if (!this.data.isRequest) return;
    app.fetchRequest({
      url: '/course/query',
      data: {
        categoryId,
        pageNum: this.data.pageNum,
        pageSize: this.data.pageSize
      },
      success: (res) => {
        if (res.list.length) {
          res.list.forEach(item => {
            item.totalDuration = formatHour(item.totalDuration);
          })
          if (this.data.courseList.length) {
            this.data.originalCourseList.push(...res.list)
            //columns 排序bug 修复核心代码
            const afterList = [].concat(...(Array.from(this.data.originalCourseList.reduce((total, cur, index) => {
              total[index % 2].push(cur)
              return total
            }, { 0: [], 1: [], length: 2 }))))
            this.setData({
              courseList: afterList,
              originalCourseList: this.data.originalCourseList,
              pageNum: ++this.data.pageNum
            })
          } else {
            const afterList = [].concat(...(Array.from(res.list.reduce((total, cur, index) => {
              total[index % 2].push(cur)
              return total
            }, { 0: [], 1: [], length: 2 }))))
            this.setData({
              courseList: afterList,
              originalCourseList: res.list,
              pageNum: ++this.data.pageNum
            })
          }
        } else {
          this.setData({
            isRequest: false
          })
        }
      }
    })
  },
  onReachBottom() {
    this.getAllCourse();
  },
  toCourse(e) {
    const courseId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `../course/course?courseId=${courseId}`,
    })
  },
  toSearch() {
    wx.navigateTo({
      url: '../search/search',
    })
  },
  onPullDownRefresh() {
    this.setData({
      pageNum: 1,
      courseList: [],
      isRequest: true
    });
    this.getBanner();
    this.getCourseCategory();
  }
})